package com.sh.two.poxy;



import com.sh.two.annotation.ShRequestLine;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 方法拦截器
 */
public class ShClientProxy implements InvocationHandler {
 
    private String baseUrl;
    private String prefix;
 
    public ShClientProxy(String baseUrl, String prefix) {
        this.baseUrl = baseUrl;
        this.prefix = prefix;
    }
 
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        ShRequestLine annotation = method.getAnnotation(ShRequestLine.class);
        String url = getUrl(annotation);   // 拿到注解里面的属性
        Object rs = mockGet(url);   // 模拟get请求
        return rs;
    }
 
    private Object mockGet(String url) {
        System.out.println("我要向这个地址发请求了" + url);
        return url.contains("username")?"我是回送的数据":"未知";
    }
 
    private String getUrl(ShRequestLine annotation) {
        return this.baseUrl + this.prefix + annotation.path();
    }
}